package com.game77.guessTheWords2.util;

import net.youmi.android.AdManager;
import net.youmi.android.AdView;
import net.youmi.android.AdViewListener;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import com.game77.guessTheWords2.MainActivity;

public class ADHelper implements AdViewListener {
	private static ADHelper Instance = new ADHelper();
	private static AdView adView;

	private ADHelper() {
	}

	public static void initAD() {

		MainActivity.instance.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				AdManager.init(MainActivity.instance, "d528067c1359fd50",
						"0bcff8080bb03aaf", 30, false);
				adView = new AdView(MainActivity.instance);
				adView.setAdViewListener(ADHelper.Instance);
				FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
						FrameLayout.LayoutParams.FILL_PARENT,
						FrameLayout.LayoutParams.WRAP_CONTENT);
				// 设置广告出现的位置(悬浮于底部)
				params.gravity = Gravity.BOTTOM;
				params.bottomMargin = 0;
				MainActivity.instance.addContentView(adView, params);
				hideAd();

			}
		});
	}

	public static void showAd() {
		if (adView != null) {
			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					adView.setVisibility(View.VISIBLE);
				}
			});
		}
	}

	public static void hideAd() {
		if (adView != null) {
			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					adView.setVisibility(View.INVISIBLE);
				}
			});
		}
	}

	@Override
	public void onAdViewSwitchedAd(AdView arg0) {

	}

	@Override
	public void onConnectFailed(AdView arg0) {

	}

}